#自定义一个过滤器  实现索引格式的转换
import functools

from flask import session, current_app, abort, g

from info.utils.models import User


def func_index_convert(value):

    index_dict = {1:"first",2:"second",3:"third"}
    return index_dict.get(value,"")

def user_login_data (f):
    @functools.wraps(f)
    def wrappers(*args,**kwargs):
    # 获取session中存储的user_id
        user_id = session.get('user_id')
        # 根据user_di 判断用户是否登陆
        user = None
        if user_id:  # 用户以登录
            try:
                user = User.query.get(user_id)
                # user = user.to_dict()  # 转换为字典（封装格式转换）
            except BaseException as e:
                current_app.logger.error(e)
                return abort(500)
        g.user= user
        return f(*args,**kwargs)
    return wrappers